using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace SingleBlog.Web
{
    using Core.Data;
    using Microsoft.EntityFrameworkCore;
    using SingleBlog.Core.Services;
    using Framework;
    using SingleBlog.UI.Filters;
    using SingleBlog.Framework.Middlewares;
    using Microsoft.Extensions.Logging;

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }


        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache();

            services.AddDataProtection();
            services.AddDbContext<SingleBlogDbContext>()

                .AddIdentity<SingleBlogUser, SingleBlogRole>(option =>
                 {
                     option.Password = new Microsoft.AspNetCore.Identity.PasswordOptions
                     {
                         RequireDigit = false,
                         RequiredLength = 6,
                         RequiredUniqueChars = 6,
                         RequireLowercase = false,
                         RequireNonAlphanumeric = false,
                         RequireUppercase = false,
                     };
                     option.SignIn = new Microsoft.AspNetCore.Identity.SignInOptions
                     {
                         RequireConfirmedEmail = false,
                         RequireConfirmedPhoneNumber = false
                     };
                     option.Lockout = new Microsoft.AspNetCore.Identity.LockoutOptions
                     {
                         AllowedForNewUsers = false,
                     };
                 })

                .AddEntityFrameworkStores<SingleBlogDbContext>()

                ;

            services
                .AddTransient<DbContext, SingleBlogDbContext>()
                .AddTransient<CategoryService>()
                .AddTransient<ArticleService>()
                .AddTransient<SettingService>()
                .AddTransient<CommentService>()
                .AddTransient<NavigationService>()
                .AddTransient<IDataInitializer,DefaultDataIntializer>()
                .AddTransient<IMessageSender, SmtpMessageSender>()
                ;


            services.AddLogging(option =>
            {
                using (var scope = services.BuildServiceProvider().CreateScope())
                {
                    option.AddProvider(new Framework.Logger.BlogLoggerProvider(scope.ServiceProvider));
                }
            });
            
            services

                .AddMvc(option =>
            {
                option.Filters.Add(typeof(InstallationCheckAttribute));
                option.Filters.Add(typeof(SiteCloseAttribute));
            })
    ;

            services.ConfigureApplicationCookie(option =>
            {
                option.LoginPath = new Microsoft.AspNetCore.Http.PathString("/Admin/Home/Login");
                option.AccessDeniedPath = option.LoginPath;
                option.LogoutPath = new Microsoft.AspNetCore.Http.PathString("/Admin/Home/Logout");                
            });
        }


        public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory loggerFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
            }

            app.UseStaticFiles();
            app.UseMiddleware<InstallationMiddleware>();
            app.UseAuthentication();

            app.UseMvc(option =>
            {
                option.MapAreaRoute("InstallRoute", "install", "Install/{action=Index}",new { controller="Install"});
                option.MapAreaRoute("AdminRoute", "admin", "admin/{controller=Home}/{action=Login}/{id?}");
                option.MapRoute("Default", "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
